﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jalal.Library.Utility;

[assembly: TagPrefix("Jalal.Library.Web.Control", "ucc")]
namespace Jalal.Library.Web.Control
{
    [ToolboxData("<{0}:DomainValidator runat=server></{0}:DomainValidator>")]
    public class DomainValidator : CustomValidator
    {
        protected override void OnPreRender(EventArgs e)
        {
            Validation validation = ValidatorFactory.GetValidator();

            TextBox control = (TextBox)Parent.FindControl(this.ControlToValidate);
            this.ClientValidationFunction = "ValidateDomainName_" + control.ClientID;

            string script = validation.GetValidateDomainNameClientScript(control.ClientID, string.IsNullOrEmpty(this.Text));
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.ClientValidationFunction, script);

            base.OnPreRender(e);
        }

        protected override bool OnServerValidate(string value)
        {
            Validation validation = ValidatorFactory.GetValidator();
            string error = string.Empty;
            this.IsValid = validation.ValidateDomainName(value, out error);
            this.ErrorMessage = error;
            return this.IsValid;
        }
    }
}
